clear all
cls

local path_results 	= "00_data/01_datasets/01_results"
local path_FTapp 	= "03_figures_tables/02_Appendix"


local Table_B1 = 1
local Table_B3 = 1
local Table_B4 = 1
local Table_B5 = 1



/// ------------------------------------------------------------------------------------------------ ///
/// ---------------------------------------- REPORTING DATES --------------------------------------- ///
/// ------------------------------------------------------------------------------------------------ ///
if `Table_B1' == 1{
	tempname table_Rdates
	file open table_Rdates using "`path_FTapp'/Table_Reporting_Dates.tex", write replace
	file write table_Rdates "\renewcommand{\arraystretch}{0.56}" _n
	file write table_Rdates "\begin{tabular}{ccc>{\centering}p{1.8cm}}" _n
	file write table_Rdates "\hline" _n
	file write table_Rdates "{\tiny{}Event} & {\tiny{}Month} & {\tiny{}Reported Day} & {\tiny{}Rate (\%)}\tabularnewline" _n
	file write table_Rdates "\hline" _n
	file write table_Rdates "{\tiny{}1} & {\tiny{}Jan-07} & {\tiny{}2/5/2007} & {\tiny{}1.14}\tabularnewline" _n
	file write table_Rdates "{\tiny{}2} & {\tiny{}Feb-07} & {\tiny{}3/5/2007} & {\tiny{}0.30}\tabularnewline" _n
	file write table_Rdates "{\tiny{}3} & {\tiny{}Mar-07} & {\tiny{}4/11/2007} & {\tiny{}0.77}\tabularnewline" _n
	file write table_Rdates "{\tiny{}4} & {\tiny{}Apr-07} & {\tiny{}5/4/2007} & {\tiny{}0.74}\tabularnewline" _n
	file write table_Rdates "{\tiny{}5} & {\tiny{}May-07} & {\tiny{}6/5/2007} & {\tiny{}0.42}\tabularnewline" _n
	file write table_Rdates "{\tiny{}6} & {\tiny{}Jun-07} & {\tiny{}7/5/2007} & {\tiny{}0.44}\tabularnewline" _n
	file write table_Rdates "{\tiny{}7} & {\tiny{}Jul-07} & {\tiny{}8/7/2007} & {\tiny{}0.50}\tabularnewline" _n
	file write table_Rdates "{\tiny{}8} & {\tiny{}Aug-07} & {\tiny{}9/7/2007} & {\tiny{}0.59}\tabularnewline" _n
	file write table_Rdates "{\tiny{}9} & {\tiny{}Sep-07} & {\tiny{}10/5/2007} & {\tiny{}0.80}\tabularnewline" _n
	file write table_Rdates "{\tiny{}10} & {\tiny{}Oct-07} & {\tiny{}11/6/2007} & {\tiny{}0.68}\tabularnewline" _n
	file write table_Rdates "{\tiny{}11} & {\tiny{}Nov-07} & {\tiny{}12/6/2007} & {\tiny{}0.85}\tabularnewline" _n
	file write table_Rdates "{\tiny{}12} & {\tiny{}Dec-07} & {\tiny{}1/7/2008} & {\tiny{}0.93}\tabularnewline" _n
	file write table_Rdates "{\tiny{}13} & {\tiny{}Jan-08} & {\tiny{}2/7/2008} & {\tiny{}0.93}\tabularnewline" _n
	file write table_Rdates "{\tiny{}14} & {\tiny{}Feb-08} & {\tiny{}3/6/2008} & {\tiny{}0.47}\tabularnewline" _n
	file write table_Rdates "{\tiny{}15} & {\tiny{}Mar-08} & {\tiny{}4/10/2008} & {\tiny{}1.13}\tabularnewline" _n
	file write table_Rdates "{\tiny{}16} & {\tiny{}Apr-08} & {\tiny{}5/9/2008} & {\tiny{}0.83}\tabularnewline" _n
	file write table_Rdates "{\tiny{}17} & {\tiny{}May-08} & {\tiny{}6/10/2008} & {\tiny{}0.56}\tabularnewline" _n
	file write table_Rdates "{\tiny{}18} & {\tiny{}Jun-08} & {\tiny{}7/11/2008} & {\tiny{}0.64}\tabularnewline" _n
	file write table_Rdates "{\tiny{}19} & {\tiny{}Jul-08} & {\tiny{}8/11/2008} & {\tiny{}0.37}\tabularnewline" _n
	file write table_Rdates "{\tiny{}20} & {\tiny{}Aug-08} & {\tiny{}9/11/2008} & {\tiny{}0.47}\tabularnewline" _n
	file write table_Rdates "{\tiny{}21} & {\tiny{}Sep-08} & {\tiny{}10/10/2008} & {\tiny{}0.51}\tabularnewline" _n
	file write table_Rdates "{\tiny{}22} & {\tiny{}Oct-08} & {\tiny{}11/11/2008} & {\tiny{}0.43}\tabularnewline" _n
	file write table_Rdates "{\tiny{}23} & {\tiny{}Nov-08} & {\tiny{}12/10/2008} & {\tiny{}0.34}\tabularnewline" _n
	file write table_Rdates "{\tiny{}24} & {\tiny{}Dec-08} & {\tiny{}1/13/2009} & {\tiny{}0.34}\tabularnewline" _n
	file write table_Rdates "\hline" _n
	file write table_Rdates "\end{tabular}{\tiny{}~~~~~~~~~~~~}%" _n
	file write table_Rdates "\begin{tabular}{ccc>{\centering}p{1.8cm}}" _n
	file write table_Rdates "\hline" _n
	file write table_Rdates "{\tiny{}Event} & {\tiny{}Month} & {\tiny{}Reported Day} & {\tiny{}Rate (\%)}\tabularnewline" _n
	file write table_Rdates "\hline" _n 
	file write table_Rdates "{\tiny{}25} & {\tiny{}Jan-09} & {\tiny{}2/11/2009} & {\tiny{}0.53}\tabularnewline" _n
	file write table_Rdates "{\tiny{}26} & {\tiny{}Feb-09} & {\tiny{}3/11/2009} & {\tiny{}0.43}\tabularnewline" _n
	file write table_Rdates "{\tiny{}27} & {\tiny{}Mar-09} & {\tiny{}4/14/2009} & {\tiny{}0.64}\tabularnewline" _n
	file write table_Rdates "{\tiny{}28} & {\tiny{}Apr-09} & {\tiny{}5/13/2009} & {\tiny{}0.33}\tabularnewline" _n
	file write table_Rdates "{\tiny{}29} & {\tiny{}May-09} & {\tiny{}6/11/2009} & {\tiny{}0.33}\tabularnewline" _n
	file write table_Rdates "{\tiny{}30} & {\tiny{}Jun-09} & {\tiny{}7/14/2009} & {\tiny{}0.42}\tabularnewline" _n
	file write table_Rdates "{\tiny{}31} & {\tiny{}Jul-09} & {\tiny{}8/12/2009} & {\tiny{}0.62}\tabularnewline" _n
	file write table_Rdates "{\tiny{}32} & {\tiny{}Aug-09} & {\tiny{}9/4/2009} & {\tiny{}0.83}\tabularnewline" _n
	file write table_Rdates "{\tiny{}33} & {\tiny{}Sep-09} & {\tiny{}10/14/2009} & {\tiny{}0.74}\tabularnewline" _n
	file write table_Rdates "{\tiny{}34} & {\tiny{}Oct-09} & {\tiny{}11/12/2009} & {\tiny{}0.80}\tabularnewline" _n
	file write table_Rdates "{\tiny{}35} & {\tiny{}Nov-09} & {\tiny{}12/11/2009} & {\tiny{}0.83}\tabularnewline" _n
	file write table_Rdates "{\tiny{}36} & {\tiny{}Dec-09} & {\tiny{}1/15/2010} & {\tiny{}0.93}\tabularnewline" _n
	file write table_Rdates "{\tiny{}37} & {\tiny{}Jan-10} & {\tiny{}2/12/2010} & {\tiny{}1.04}\tabularnewline" _n
	file write table_Rdates "{\tiny{}38} & {\tiny{}Feb-10} & {\tiny{}3/12/2010} & {\tiny{}1.25}\tabularnewline" _n
	file write table_Rdates "{\tiny{}39} & {\tiny{}Mar-10} & {\tiny{}4/14/2010} & {\tiny{}1.14}\tabularnewline" _n
	file write table_Rdates "{\tiny{}40} & {\tiny{}Apr-10} & {\tiny{}5/12/2010} & {\tiny{}0.83}\tabularnewline" _n
	file write table_Rdates "{\tiny{}41} & {\tiny{}May-10} & {\tiny{}6/14/2010} & {\tiny{}0.75}\tabularnewline" _n
	file write table_Rdates "{\tiny{}42} & {\tiny{}Jun-10} & {\tiny{}7/14/2010} & {\tiny{}0.73}\tabularnewline" _n
	file write table_Rdates "{\tiny{}43} & {\tiny{}Jul-10} & {\tiny{}8/13/2010} & {\tiny{}0.80}\tabularnewline" _n
	file write table_Rdates "{\tiny{}44} & {\tiny{}Aug-10} & {\tiny{}9/15/2010} & {\tiny{}0.74}\tabularnewline" _n
	file write table_Rdates "{\tiny{}45} & {\tiny{}Sep-10} & {\tiny{}10/15/2010} & {\tiny{}0.72}\tabularnewline" _n
	file write table_Rdates "{\tiny{}46} & {\tiny{}Oct-10} & {\tiny{}11/12/2010} & {\tiny{}0.84}\tabularnewline" _n
	file write table_Rdates "{\tiny{}47} & {\tiny{}Nov-10} & {\tiny{}12/16/2010} & {\tiny{}0.73}\tabularnewline" _n
	file write table_Rdates "{\tiny{}48} & {\tiny{}Dec-10} & {\tiny{}1/14/2011} & {\tiny{}0.84}\tabularnewline" _n
	file write table_Rdates "\hline " _n
	file write table_Rdates "\end{tabular}{\tiny\par}" _n
	file close table_Rdates
}
// ------------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------- //
// END OF `Table_B3' == 1


/// ------------------------------------------------------------------------------------------------ ///
/// ---------------------------------------- VARIANCE TESTS ---------------------------------------- ///
/// ------------------------------------------------------------------------------------------------ ///
if `Table_B3' == 1{
	  
	* Load results for 2-day window  
    use "`path_results'/10_VarianceTest_Window_0_Year_2007.dta", clear // Results for the 2-day window (Window==0)
	gen n = _n
	rename VarianceTest1 Test_2day

	* Merge with 3-day window
	merge 1:1 Labels using "`path_results'/10_VarianceTest_Window_1_Year_2007.dta" // Results for the 3-day window (Window==1)
	rename VarianceTest1 Test_3day
	drop _merge
		
	
	* Merge with Smaller Window (2-day)
	merge 1:1 Labels using "`path_results'/10_VarianceTest_Window_2_Year_2007.dta" // Results for the small 2-day window (Window==2)
	rename VarianceTest1 Test_SmallWindow_2day
	drop _merge
	
	* Merge with Smaller Window (3-day)
	merge 1:1 Labels using "`path_results'/10_VarianceTest_Window_3_Year_2007.dta" // Results for the small 3-day window (Window==3)
	rename VarianceTest1 Test_SmallWindow_3day
	
	order Labels Test_2day Test_3day Test_SmallWindow_2day Test_SmallWindow_3day
	sort n
	drop n _merge

	tempname table_Ftest
	file open table_Ftest using "`path_FTapp'/Ftest_heterosk_Year_2007.tex", write replace
	file write table_Ftest "\renewcommand{\arraystretch}{0.95}"
	file write table_Ftest "\begin{tabular}{lcccc}" _n "\hline \hline" _n
	file write table_Ftest " &" "Window 1" "&" "Window 2" "&" "Window 3" "&" "Window 4"  "\\" _n
	file write table_Ftest "\hline \hline" _n
	file write table_Ftest "\textit{Window Type} \\" _n
	file write table_Ftest "\quad Event" 		"&" "2-day window"    "&" "3-day window"   "  &" "2-day window"  " &" "3-day window"         "\\" _n
	file write table_Ftest "\quad Non-event"	"&" "All other days"  "&" "All other days" "  &" "4-day window"  " &" "4-day window"         "\\" _n
	file write table_Ftest "\cline{1-5}" _n
	file write table_Ftest "\textit{Standard Deviation} \\" _n
	file write table_Ftest "\quad Event" 		"&" (string(Test_2day[4], "%5.3f")) "&" (string(Test_3day[4], "%5.3f")) " &" (string(Test_SmallWindow_2day[4], "%5.3f"))  " &" (string(Test_SmallWindow_3day[4], "%5.3f"))           "\\" _n
	file write table_Ftest "\quad Non-event" 	"&" (string(Test_2day[5], "%5.3f")) "&" (string(Test_3day[5], "%5.3f")) " &" (string(Test_SmallWindow_2day[5], "%5.3f"))  " &" (string(Test_SmallWindow_3day[5], "%5.3f"))           "\\" _n
	file write table_Ftest "\cline{1-5}" _n
	file write table_Ftest "\textit{Ratio Test: $\sigma_{\Delta{BE},E}>\sigma_{\Delta{BE},NE}$} \\" _n
	file write table_Ftest "\cline{1-5}" _n
	file write table_Ftest "\textit{F-test} \\" _n
	file write table_Ftest "\quad F-value" 			"&" (string(Test_2day[2], "%5.3f")) "&" (string(Test_3day[2], "%5.3f"))  " &" (string(Test_SmallWindow_2day[2], "%5.3f"))  " &" (string(Test_SmallWindow_3day[2], "%5.3f"))        "\\" _n
	file write table_Ftest "\quad $ P(F>f)$"        "&" (string(Test_2day[1], "%5.3f")) "&" (string(Test_3day[1], "%5.3f"))  " &" (string(Test_SmallWindow_2day[1], "%5.3f"))  " &" (string(Test_SmallWindow_3day[1], "%5.3f"))        "\\" _n
	file write table_Ftest "\textit{BC Bootstrap - One-Sided CI} \\" _n
	file write table_Ftest "\quad 90\% CI Lower Bound"  "&" (string(Test_2day[8], "%5.3f"))  "&" (string(Test_3day[8], "%5.3f")) " &" (string(Test_SmallWindow_2day[8], "%5.3f"))  " &" (string(Test_SmallWindow_3day[8], "%5.3f"))        "\\" _n
	file write table_Ftest "\quad 95\% CI Lower Bound"  "&" (string(Test_2day[10], "%5.3f"))  "&" (string(Test_3day[10], "%5.3f")) " &" (string(Test_SmallWindow_2day[10], "%5.3f"))  " &" (string(Test_SmallWindow_3day[10], "%5.3f"))        "\\" _n
	file write table_Ftest "\hline \hline" _n
	file write table_Ftest "\end{tabular}" _n
	file close table_Ftest

}
// ------------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------- //
// END OF `Table_B3' == 1



/// ------------------------------------------------------------------------------------------------ ///
/// --------------------------------------- OLS REGRESSIONS ---------------------------------------- ///
/// ------------------------------------------------------------------------------------------------ ///
if `Table_B4'==1 { 
	foreach sample_year of numlist 2007 2010 {

	    * Load Results	  
		   use "`path_results'/30_OLS_Regression_FullSample_Window_0_Year_`sample_year'", clear   //2-day window
		   gen window ="3-day window"
		   append using "`path_results'/30_OLS_Regression_FullSample_Window_1_Year_`sample_year'" //3-day window
		   replace window ="5-day window" if window==""
		   gen stars = ""
		   replace stars = "*"   if pval<0.1
		   replace stars = "**"  if pval<0.05
		   replace stars = "***" if pval<0.01
		   gen beta_star= string(beta,"%5.3f")+stars
		   gen se_string = "("+string(se,"%5.3f")+")"
		   drop if _n==4
		
		* Create the Table
		tempname table_OLSreg
		file open table_OLSreg using "`path_FTapp'/OLS_Regression_Year_`sample_year'.tex", write replace
		file write table_OLSreg "\begin{tabular}{lccccc}" _n "\hline \hline" _n
		file write table_OLSreg " & (1) & (2) & (3) & (4) & (5) \\" _n  "\hline" _n	
		file write table_OLSreg "Event Window      &  Full Sample             &  \multicolumn{2}{c}{2-day Window}   & \multicolumn{2}{c}{3-day Window}  \\" _n	
		file write table_OLSreg "\hline \hline" _n
		file write table_OLSreg "$\Delta BE$" 	    "&" (beta_star[1])  " &" (beta_star[2]) " &" (beta_star[3]) " &" (beta_star[4])  " &" (beta_star[5])  " \\" _n
		file write table_OLSreg "Standard Error"    "&" (se_string[1])  " &" (se_string[2]) " &" (se_string[3]) " &" (se_string[4])  " &" (se_string[5])  " \\" _n
		file write table_OLSreg "Observations" "&" (string(obs[1], "%5.0f")) "&" (string(obs[2], "%5.0f")) "&" (string(obs[3], "%5.0f")) "&" (string(obs[4], "%5.0f"))  "&" (string(obs[5], "%5.0f")) "\\"  _n
		file write table_OLSreg "\hline" _n
		file write table_OLSreg "Days Included     & All   & Event Days   & Non-Event Days & Event Days   & Non-Event Days \\" _n
		file write table_OLSreg "Controls & Yes & Yes & Yes  & Yes & Yes\\" _n	
		file write table_OLSreg "\hline \hline" _n
		file write table_OLSreg "\end{tabular}" _n
		file close table_OLSreg
	}
}
// ------------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------- //
// END OF `Table_B4' == 1



/// ------------------------------------------------------------------------------------------------ ///
/// ----------------------------------------- EVENT STUDY ------------------------------------------ ///
/// ------------------------------------------------------------------------------------------------ ///
if `Table_B5'==1 {
	*Load Results for the full sample (alpha & number of observations)
	use "`path_results'/40_EventStudy_Window_0_Year_2007.dta", clear // Results for the 2-day window (Window==0)
	gen year = 2007
	append using "`path_results'/40_EventStudy_Window_0_Year_2010.dta"
	tempname table_EventStudy
	file open table_EventStudy using "`path_FTapp'/EventStudy_2dayWindow.tex", write replace
	file write table_EventStudy "\renewcommand{\arraystretch}{1.}"
	file write table_EventStudy "\begin{tabular}{lcccccc}" _n "\hline \hline" _n
	file write table_EventStudy "Event Type & \# Events & Obs & $\bar{\Delta ln(SP^A)}$ & J1-stat & $\bar{\Delta BE}$ \\" _n
	file write table_EventStudy "\cline{1-6}" _n
	///
	file write table_EventStudy "\multicolumn{6}{c}{2007-2008} \\" _n
	file write table_EventStudy "Good News Event"  "&" (string(Events[1], "%5.0f")) " &" (string(Obs[1], "%5.0f"))  " &" (string(AR_mean[1], "%5.3f")) " &" (string(Jstat[1], "%5.3f")) " &" (string(BE_mean[1], "%5.3f"))    " \\" _n
	file write table_EventStudy "Bad News Event"  "&"  (string(Events[3], "%5.0f")) "&"  (string(Obs[3], "%5.0f"))  " &" (string(AR_mean[3], "%5.3f")) " &" (string(Jstat[3], "%5.3f")) " &" (string(BE_mean[3], "%5.3f"))     " \\" _n
    ///
	file write table_EventStudy "\multicolumn{6}{c}{2010-2011} \\" _n
	file write table_EventStudy "Good News Event"  "&" (string(Events[4], "%5.0f")) " &" (string(Obs[4], "%5.0f"))  " &" (string(AR_mean[4], "%5.3f")) " &" (string(Jstat[4], "%5.3f")) " &" (string(BE_mean[4], "%5.3f"))     " \\" _n
	file write table_EventStudy "Bad News Event"  "&"  (string(Events[6], "%5.0f")) "&"  (string(Obs[6], "%5.0f"))  " &" (string(AR_mean[6], "%5.3f")) " &" (string(Jstat[6], "%5.3f")) " &" (string(BE_mean[6], "%5.3f"))     " \\" _n
	///
	file write table_EventStudy "\hline \hline" _n
	file write table_EventStudy "\end{tabular}" _n
	file close table_EventStudy

}
// ------------------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------------------- //
// END OF `Table_B5' == 1
